// Author: wsfuyibing <682805@qq.com>
// Date: 2025-04-28

package services

import (
	"context"
	"gitee.com/go-libs/db-xorm/db"
	"server/app/errs"
	"server/app/http/models"
	"server/app/kind"
)

// ListByIdList
// 按用户ID列表查询用户列表.
func (o *UsersService) ListByIdList(ctx context.Context, conditions []kind.UserId) (list []*models.Users, err error) {
	var sess *db.Session

	// 1. 获取连接.
	if sess, err = o.Dao.Slaver(ctx); err != nil {
		err = errs.ErrDatabaseQuery
		return
	}

	// 2. 主键查询.
	if err = sess.In("id", conditions).Find(&list); err != nil {
		err = errs.ErrDatabaseQuery
	}
	return
}

// MapByIdList
// 按用户ID列表查询用户列表.
func (o *UsersService) MapByIdList(ctx context.Context, conditions []kind.UserId) (mapper map[kind.UserId]*models.Users, err error) {
	var list []*models.Users
	if list, err = o.ListByIdList(ctx, conditions); err == nil {
		mapper = make(map[kind.UserId]*models.Users)
		for _, v := range list {
			mapper[v.Id] = v
		}
	}
	return
}

func (o *UsersService) UpdateAvatar(ctx context.Context, model *models.Users) (affects int64, err error) {
	return o.Dao.UpdateFieldsById(ctx, map[string]any{
		"avatar": model.Avatar,
	}, model.Id)
}

func (o *UsersService) UpdateGender(ctx context.Context, model *models.Users) (affects int64, err error) {
	return o.Dao.UpdateFieldsById(ctx, map[string]any{
		"gender": model.Gender,
	}, model.Id)
}

func (o *UsersService) UpdateNickname(ctx context.Context, model *models.Users) (affects int64, err error) {
	return o.Dao.UpdateFieldsById(ctx, map[string]any{
		"nickname": model.Nickname,
	}, model.Id)
}
